<?php


namespace app\home\service;

use think\Cache;

class Huawei
{

    private $client_id = null;
    private $client_secret = null;

    public function __construct($client_id,$client_secret)
    {
        $this->client_id = $client_id;
        $this->client_secret = $client_secret;
    }

    public function get_token(){

        $huaweitoken = Cache::get('huaweitoken');
        if(!empty($huaweitoken)){
            return  $huaweitoken;
        }


        $url = "https://oauth-login.cloud.huawei.com/oauth2/v3/token";
        $data['grant_type'] = 'client_credentials';
        $data['client_id'] = $this->client_id;
        $data['client_secret'] = $this->client_secret;

        $curlPost = http_build_query($data);
        $headers = array(
            "Content-Type: application/x-www-form-urlencoded",
        );

        $postUrl = $url;
        $curl = curl_init(); //初始化curl
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $postUrl); //抓取指定网页
        curl_setopt($curl, CURLOPT_HEADER, 0); //设置header
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_POST, 1); //post提交方式
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost); //提交的参数
        $json = curl_exec($curl); //运行curl
        curl_close($curl);
        mylog($json);
        if($json == false){
            mylog('token curl fail');
            return false;
        }

        $res = json_decode($json,true);
        if($res['responseCode'] != 0){
            mylog('token fail');
            return false;
        }

        $json = json_decode($json,true);

        Cache::set('huaweitoken',$json['access_token'],$json['expires_in']-60*10);

        return $json['access_token'];
    }

    /**
     * 通知华为确认购买
     *
     * @param [type] $purchaseToken
     * @param [type] $productId
     * @return boolean
     */
    public function confirm_order($purchaseToken,$productId){
        $url = "https://orders-drcn.iap.hicloud.com/applications/v2/purchases/confirm";
        $data = array('purchaseToken'=>$purchaseToken,'productId'=>$productId);
        $curlPost = json_encode($data);

        $headers = array(
            "Content-Type: application/json; charset=UTF-8",
            "Authorization: Basic " . base64_encode('APPAT:'.$this->get_token())
        );

        $postUrl = $url;
        $curl = curl_init();//初始化curl
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);//提交的参数
        $return = curl_exec($curl);//运行curl
        curl_close($curl);
        mylog("通知地址：{$url}");
        mylog("华为确认订单结果：");
        mylog($return);
        return true;
    }

    public function verify_order($purchaseToken,$productId){
        $url = "https://orders-drcn.iap.hicloud.com/applications/purchases/tokens/verify";
        $data = array('purchaseToken'=>$purchaseToken,'productId'=>$productId);
        $curlPost = json_encode($data);

        $headers = array(
            "Content-Type: application/json; charset=UTF-8",
            "Authorization: Basic " . base64_encode('APPAT:'.$this->get_token())
        );

        $postUrl = $url;
        $curl = curl_init();//初始化curl
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);//提交的参数
        $return = curl_exec($curl);//运行curl
        mylog("通知地址：{$url}");
        mylog("华为token结果：");
        mylog($return);
        curl_close($curl);

        return $return;


        if($return == false){
            mylog($return);
            return false;
        }

        $response_array = json_decode($return,true);
        if($response_array['responseCode'] != 0){
            return false;
        }

        return true;
    }
}